// pages/newSigned/newSigned.js
import Dialog from '../../miniprogram_npm/@vant/weapp/dialog/dialog';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    codes: "",
    type: 'back',
    title: "新签约企业"
  },
  inputInvitation(e) {
    let {
      value
    } = e.detail || {};
    this.setData({
      codes: value
    })
  },
  sign() {
    wx.showLoading({
      title: '加载中...',
      mask: true,
      icon: "none"
    })
    wx.$api.contractEnterprise({
      invite_code: this.data.codes
    }).then(res => {
      wx.hideLoading();
      if (res.data.code == '200') {
        let {
          is_auth,
          enterprise_id,
          is_contract,
          enterprise_professional_facilitator_id
        } = res.data.data || {};
        wx.setStorageSync('is_auth', is_auth);
        wx.setStorageSync('is_contract', is_contract);
        if (is_auth == 1) {
          wx.reLaunch({
            url: '/pages/business/business?id=' + res.data.data.enterprise_professional_facilitator_id,
          })
        } else {
          wx.$Toast(res.data.msg);
        }
      } else if (res.data.code == '502') {
        wx.$Toast(res.data.msg);
        if (!res.data.data.sign_img) {
          Dialog.confirm({
            title: '提示',
            message: '您尚未签署协议，请尽快完成',
            asyncClose: true,
            showCancelButton: true,
            confirmButtonText: '签署协议',
            cancelButtonText: '回首页',
            className: 'btnWidth'
          }).then(() => {
            Dialog.close();
            setTimeout(() => {
              wx.reLaunch({
                url: '/pages/business/business?id=' + res.data.data.enterprise_professional_facilitator_id,
              })
            }, 200)
          }).catch(() => {
            Dialog.close();
            setTimeout(() => {
              wx.switchTab({
                url: '/pages/index/index',
              })
            }, 200)
          });
        } else {
          setTimeout(() => {
            wx.switchTab({
              url: '/pages/index/index',
            })
          }, 1000)
        }
      } else {
        wx.$Toast(res.data.msg);
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    if (options.codes != undefined) { //存在邀请码 option
      this.setData({
        codes: options.codes
      });
    }

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  }
})